package com.example.minefieldkeepercloud.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.minefieldkeepercloud.LoginAc;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.bean.VersionBean;
import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.OtherUitl;
import com.example.minefieldkeepercloud.util.SPUtilHelper;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.util.imageUtil.GlideUtil;
import com.example.minefieldkeepercloud.view.dialog.UpdateDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的
 */
public class MyActivity extends AppCompatActivity {


    @BindView(R.id.iv_pic)
    ImageView iv_pic;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
   @BindView(R.id.tv_version)
    TextView tv_version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ButterKnife.bind(this);

        initView();
    }

    @OnClick({R.id.title_left, R.id.tv_version, R.id.tv_finish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_version:

                getVersionInfo();
                break;


            case R.id.title_left:
                //取消
                finish();
                break;

            case R.id.tv_finish:
                //退出系统
                Intent intent = new Intent(MyActivity.this, LoginAc.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;


        }
    }

    private void initView() {

        GlideUtil.loadCircleImage(R.mipmap.headpicss, iv_pic);
        tv_name.setText(SPUtilHelper.getName());
        tv_phone.setText(SPUtilHelper.getPhone());
        tv_version.setText(getVerName(this));
    }


    /**
     * 版本跟新
     */
    private void getVersionInfo() {
        NetHelperNew.GetVersionr(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {

                Log.i("-->", "版本更新：" + data);
                VersionBean baseBean = MyGson.getInstance().fromJson(data, VersionBean.class);
                if (baseBean.getType() == 1) {
                    getIsUpdate(baseBean.getData().getVersionCode(), baseBean.getData().getFileName(), baseBean.getData().getUpdateInfo());
                }

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);

                ToastUtil.showToast(MyActivity.this,msg);
            }
        });

    }

    /**
     * 版本更新
     *
     * @param versioncode 版本号
     * @param url         下载地址
     */
    private void getIsUpdate(String versioncode, String url, String versioninfo) {
        if (versioncode.length() > 0)
            if (Integer.valueOf(versioncode) > Integer.valueOf(OtherUitl.getVersionCode(MyActivity.this))) {
                UpdateDialog updateDialog = new UpdateDialog(MyActivity.this, R.style.dialog, versioninfo, url);
                updateDialog.show();
            } else {
                ToastUtil.showToast(MyActivity.this, "当前已经是最新版本");
            }


    }

    /**
     * 获取版本名称
     *
     * @param context
     * @return
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(
                    "com.example.minefieldkeepercloud", 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {

        }
        return verName;
    }
}
